package com.ttbj.controller.costsharing;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.costsharing.CostSharingConfigService;
import com.ttbj.api.dto.CostSharingConfigDto;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.util.ViewDataUtil;



@Controller
public class CostSharingConfigController {
	
	@Autowired
	private CostSharingConfigService costSharingConfigService;
	
	
	/**
	 * 分成配置页面
	 * @return
	 */
	@RequestMapping("/costsharing/cost_sharing_config.htm")
	public String cost_sharing() {
		return "costsharing/cost_sharing_config";
	}
	
	
	/**
	 * 分成配置信息的查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/costsharing/getCostSharingConfigInfo.htm", method=RequestMethod.GET, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getCostSharingConfigInfo(HttpServletRequest request) {
		List<CostSharingConfigDto> costSharingManager = this.costSharingConfigService.findCostSharingConfigInfo();
		if (null != costSharingManager && costSharingManager.size() > 0) {
			return ViewDataUtil.buildSuccessJson("分成配置信息", costSharingManager);
		}
		return ViewDataUtil.buildFailureJson(1, "配置信息不存在");
	}
	
	
	/**
	 * 修改分配规则
	 * @param request
	 * @param driverType 司机分类：1为短运司机，2为长运司机
	 * @param platformCostSharing 平台
	 * @param companyCostSharing 公司
	 * @param driverCostSharing 司机
	 * @return
	 */
	@RequestMapping(value="/costsharing/modifyCostSharingConfigInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String modifyCostSharingConfigInfo(HttpServletRequest request, @RequestParam Integer driverType, @RequestParam Double platformCostSharing,
										@RequestParam Double companyCostSharing, @RequestParam Double driverCostSharing) {
		
		//正整数和0 的正则表达式
		String regEx = "^\\d+(\\.\\d+)?$";
		// 编译正则表达式
		Pattern pattern = Pattern.compile(regEx);
		//验证平台分配的数值
		boolean pc = pattern.matcher(platformCostSharing.toString()).matches();
		//验证公司分配的数值
		boolean cc = pattern.matcher(companyCostSharing.toString()).matches();
		//验证司机分配的数值
		boolean dc = pattern.matcher(driverCostSharing.toString()).matches();
		//如果有一个没有验证通过，则直接返回提示信息
		if (!pc || !cc || !dc) {
			return ViewDataUtil.buildFailureJson(PlatFormErrorCode.DATA_SECURITY.code(), "只能填写0和正整数");
		}
		//获取三个的总数
		Double sum = platformCostSharing + companyCostSharing + driverCostSharing;
		//如果3个输入的数大于 100 ，则直接返回提示信息
		if (sum > 100) {
			return ViewDataUtil.buildFailureJson(PlatFormErrorCode.DATA_SECURITY.code(), "输入值的总和不能大于100");
		}
		CostSharingConfigDto cDto = new CostSharingConfigDto();
		cDto.setPlatformCostSharing(platformCostSharing);
		cDto.setCompanyCostSharing(companyCostSharing);
		cDto.setDriverCostSharing(driverCostSharing);
		cDto.setDriverType(driverType);
		this.costSharingConfigService.editCostSharingConfigInfo(cDto);
		return ViewDataUtil.buildSuccessJson("修改成功!");
	}
	
}
